/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package io.iec.edp.caf.msu.config;

import com.alibaba.boot.nacos.discovery.autoconfigure.NacosDiscoveryAutoConfiguration;
import io.iec.edp.caf.impl.ServiceUnitAwareServiceImpl;
import io.iec.edp.caf.msu.api.MsuService;
import io.iec.edp.caf.msu.api.ServiceUnitAwareService;
import io.iec.edp.caf.msu.api.client.ServiceDiscovery;
import io.iec.edp.caf.msu.api.client.ServiceRegistry;
import io.iec.edp.caf.msu.api.entity.MsuProperties;
import io.iec.edp.caf.msu.client.event.ServiceRegistryListener;
import io.iec.edp.caf.msu.common.MsuServiceImpl;
import io.iec.edp.caf.msu.common.ServiceUnitAwareImpl;
import io.iec.edp.caf.msu.common.SuService;
import io.iec.edp.caf.sumgr.api.IServiceUnitAware;
import io.iec.edp.caf.sumgr.api.SUService;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 配置类
 *
 * @author guowenchang
 * @date 2020-06-05
 */
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(MsuProperties.class)
@AutoConfigureBefore(NacosDiscoveryAutoConfiguration.class)
public class MsuServiceAutoConfiguration {

    //为了防止nacos被自动激活 注册一个空bean
    @ConditionalOnExpression("!${msu.enable:false} or '${msu.deployMode}'=='Kubernetes'")
    @Bean("globalNacosProperties$discovery")
    public Object preventNacos() {
        return new Object();
    }

    @Bean
    public ApplicationListener msuStartupListener(ServiceRegistry serviceRegistry, MsuProperties msuProperties) {
        return new ServiceRegistryListener(serviceRegistry, msuProperties);
    }

    @Bean
    public ServiceUnitAwareService serviceUnitAwareImpl() {
        return new ServiceUnitAwareServiceImpl();
    }

    @Bean
    public IServiceUnitAware iserviceUnitAware() {
        return new ServiceUnitAwareImpl();
    }

    @Bean
    @ConditionalOnMissingBean({MsuService.class})
    public MsuService createMSUService(ServiceUnitAwareService suAware, ServiceDiscovery serviceDiscovery) {
        return new MsuServiceImpl(suAware, serviceDiscovery);
    }

    @Bean
    public SUService createSUService(MsuService msuService) {
        return new SuService(msuService);
    }

}
